package com.chainshop.restock.service.impl;

import cn.hutool.core.lang.intern.InternUtil;
import com.chainshop.common.core.domain.pojo.RestockProduct;
import com.chainshop.common.core.domain.pojo.Stock;
import com.chainshop.common.core.domain.pojo.StoreInfo;
import com.chainshop.common.exception.ServiceException;
import com.chainshop.common.utils.SecurityUtils;
import com.chainshop.restock.constant.RestockConstants;
import com.chainshop.restock.mapper.RestockStockMapper;
import com.chainshop.restock.mapper.StoreInfoMapper;
import com.chainshop.restock.service.IRestockStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.chainshop.common.utils.PageUtils.startPage;

/**
 * @description:
 */
@Service
public class RestockStoreServiceImpl implements IRestockStoreService {

    @Autowired
    private RestockStockMapper restockStockMapper;

    @Autowired
    private StoreInfoMapper storeInfoMapper;

    @Override
    public List<Stock> selectStockList(Stock stock) {
        startPage();
        List<Stock> stocks = restockStockMapper.selectStockList(stock);
        return stocks;
    }

    @Override
    public Stock selectStockById(Integer id) {
        return restockStockMapper.selectStockById(id);
    }

    @Override
    public int updateStock(Stock stock) {
        return restockStockMapper.updateStock(stock);
    }

    @Override
    public List<StoreInfo> getStores() {
        List<StoreInfo> storeInfos = storeInfoMapper.getStores();
        return storeInfos;
    }

    @Override
    public StoreInfo selectStoreById(Integer id) {
        return storeInfoMapper.selectStoreById(id);
    }

    @Override
    public StoreInfo getStoreByUserId() {
        Long userId = SecurityUtils.getUserId();
        return storeInfoMapper.selectStoreByUserId(userId);
    }

    @Override
    public List<Stock> selectStockListByProductId(RestockProduct restockProduct) {

        // 确保 restockProduct 不为 null
        if (restockProduct == null || restockProduct.getProductId() == null) {
            throw new IllegalArgumentException("系统出错，请及时响应给管理员");
        }

        List<Stock> stocks = restockStockMapper.selectStockListByProductId(restockProduct.getProductId());



        if (Objects.nonNull(stocks) && !stocks.isEmpty()) {
            stocks.forEach(stock -> {
                if (Objects.nonNull(stock) && Objects.nonNull(stock.getStockQty())) {
                    if ((stock.getStockQty() - restockProduct.getQuantity()) < RestockConstants.RESTOCK_STATUS_STOCK_ALERT_COUNT) {
                        stock.setStockStatus(RestockConstants.RESTOCK_STATUS_STOCK_ALERT);
                    }else {
                        stock.setStockStatus(RestockConstants.RESTOCK_STATUS_STOCK_NORMAL);
                    }
                }
            });
        }else {
            throw new ServiceException("没有满足，商品调度的库存，请及时提交给采购员");
        }
        return stocks;
    }

    /**
     * 批量删除存储库存信息
     *
     * @param ids 需要删除的存储库存信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStockByIds(Integer[] ids)
    {
        return restockStockMapper.deleteStockByIds(ids);
    }

    /**
     * 删除存储库存信息信息
     *
     * @param id 存储库存信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteStockById(Integer id)
    {
        return restockStockMapper.deleteStockById(id);
    }

}
